package com.eascs.archive_query.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.api.form.O2oAfterLoanForm;
import com.eascs.admin.o2o.internal.service.PubDditemService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.specialInterview.service.O2OSpecialInterviewService;
import com.eascs.afterloan.service.AdvanceSettleService;
import com.eascs.afterloan.service.O2oAfterService;
import com.eascs.afterloancheck.service.LoanCheckServicce;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.crm.nsEditDetail.service.CustomerAssetInfoService;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;

@RestController
@RequestMapping("/rarchive_query")
public class Archive_QueryRestController {
	@Reference(retries = 3)
	ICustomerInfoService iCustomerInfoService;
	//	@Autowired
//	private O2oSdService o2oSdService;
//	@Autowired
//	private OaLoanapplyService oaLoanapplyService;
//	@Inject
//	private O2oPaymentsService o2oPaymentsService;
	@Value("${o2o.admin.iframe.domain}")
	private String domain;
	@Value("${sd.url}")
	private String sdUrl;
	@Value("${oa.search.url}")
	private String oaUrl;
	//	@Autowired
//	private OaLoanapplyDetailService oaDetailService;
	@Value("${oa.zx.url}")
	private String oaZxUrl;
	@Value("${oa.ns.url}")
	private String oaNsUrl;
	@Value("${oa.yh.url}")
	private String oaYhUrl;
	@Value("${oa.fk.url}")
	private String oaFkUrl;
	@Value("${oa.bzj.url}")
	private String oaBzjUrl;
	//	@Autowired
//	private O2oOverdueService o2oOverdueService;
	@Autowired
	LoanCheckServicce loanCheckServicce;
	@Autowired
	O2OSpecialInterviewService o2OSpecialInterviewService;
	@Autowired
	AdvanceSettleService advanceSettleService;
	@Autowired
	CustomerShopInfoService shopInfoService;
	@Autowired
	CustomerAssetInfoService assetInfoService;
	@Autowired
	PubDditemService pubDditemService;
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	@Autowired
	O2oAfterService service;
	@Autowired
	O2oDataListService o2oDataListService;
	@Reference
	SysOrgService sysOrgService;

	@RequestMapping("/o2o")
	public PaginationRestResult o2o(O2oAfterLoanForm entity) {
		String o2ourl = "/rarchive_query/o2o";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();// O2oAfterLoanForm中的借款人姓名//
																									// loanUserName
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();

			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);

			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10);

		List<User> users = sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no = "";
		if (users != null) {
			for (User user : users) {
				no += user.getNo() + ",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length() - 1, no.length()))) {
				no = no.substring(0, no.length() - 1);
			}
		}
		entity.setUsersno(no);
		PageResult<Object[]> pageResult = service.findArchive_QueryByPage(entity);//
		pageResult.getData();
		String titles[] = { "贷款单号", " 借款人姓名", "信贷经理", "放款银行", "经营平台", "省区", "贷款类型", "放款金额(万元)", "放款日期", "到期日期 ",
				"还款状态" };
		// 添加详情按钮 一期以后再加
		Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, "/archiveDetail?billCode={1}") };// 详情按钮
																													// //billCode前面还有个主键
		SelectDataOption[] selectDataOptions = { null, null, null, new SelectDataOption("BANKTYPEO2O"), null, null,
				new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, new SelectDataOption("HKZT") };

		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		// 设置详情按钮
		if (entity.getMenutype() == null) {
			o2oPagingInfo.setTitleButtons(titleButtons);
		}
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null, null, null, new DecimalFormat(",##0.00"),
				null, null, null });
		o2oPagingInfo.setRendTo("#result_list");// index.ftl文件中 <div
												// id="result_list"
												// class="action-ajax-load mt20"
												// data-url="${o2ourl}"></div>
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}

}
